﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGameAula02
{
    class ShotManager
    {
        private List<Shot> shots;
        public float timeBetweenShots;
        public float ShotLifeTime;

        private EntityType amigavel;
        public EntityType Amigavel
        {
            get { return amigavel; }
        }
        private EntityType inimigo;
        public EntityType Inimigo
        {
            get { return inimigo; }
        }

        private Texture2D image;
        private float canShot;
        private Animation[] a;
        public ShotManager(Texture2D image, Animation[] a)
        {
            this.image = image;
            this.a = a;
            shots = new List<Shot>();
        }

        public void NewShot(GameTime gT, Player p, EntityType tipo)
        {
            Shot s;
            if (canShot > (float)gT.TotalGameTime.TotalMilliseconds)
            {
                return;
            }
            s = new Shot(image, p.Position, p.Rotation , p.Speed, canShot, a);
            s.MyEntity = tipo;
            shots.Add(s);
            canShot += timeBetweenShots;
        }
        public void Update(GameTime gT)
        {
            if (shots == null) return;
            for (int i = shots.Count-1; i >= 0; i-- )
            {
                shots[i].UpdatePosition();
                shots[i].Update(gT);
                if ((float)gT.TotalGameTime.TotalMilliseconds >= shots[i].creationTime + ShotLifeTime)
                {
                    shots.RemoveAt(i);
                }else{

                }
            }

            Level1.SceneObjects.RemoveAll(SceneObjectType);
            foreach (Shot s in shots)
            {
                Level1.SceneObjects.Add(s);
            }

            CheckCollision();

        }
        public bool SceneObjectType(AnimatedObject2D ao)
        {
            return typeof(Shot) == ao.GetType(); 
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gt)
        {
            if (shots == null) return;
            foreach (Shot s in shots)
            {
                s.Draw(spriteBatch, gt);
            }
        }
        
        public void CheckCollision()
        {

            for (int j = Level1.SceneObjects.Count - 1; j >= 0; j--)
            {

                for (int i = shots.Count - 1; i >= 0; i--)
                {
                    if (shots[i].MyEntity == EntityType.Friendly)
                    {
                        if (Level1.SceneObjects[j].MyEntity == EntityType.Hositile && shots[i].CheckCollision(Level1.SceneObjects[j]))
                        {
                            Level1.SceneObjects.RemoveAt(j);
                            //Level1.SceneObjects.Remove(shots[i]);
                            shots.RemoveAt(i);
                        }
                    }
                    if (shots[i].MyEntity == EntityType.Hositile)
                    {
                        if (Level1.SceneObjects[j].MyEntity == EntityType.Friendly && shots[i].CheckCollision(Level1.SceneObjects[j]))
                        {
                            Level1.SceneObjects.RemoveAt(j);
                            //Level1.SceneObjects.Remove(shots[i]);
                            shots.RemoveAt(i);
                        }
                    }

                }

            }

        }

    }
}
